package org.example.yunshu.demos.service;

import lombok.extern.slf4j.Slf4j;
import org.example.yunshu.demos.constant.Constants;
import org.example.yunshu.demos.constant.HttpSessionNullException;
import org.example.yunshu.demos.mapper.FriendMapper;
import org.example.yunshu.demos.mapper.SessionMapper;
import org.example.yunshu.demos.mapper.UserMapper;
import org.example.yunshu.demos.model.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SessionMapper sessionMapper ;
    @Autowired
    private FriendMapper friendMapper ;
   // 万能验证码钥匙
    private String generalCaptcha = "xieyeqi" ;

    public Object login(UserInfo userInfo, String inputCaptcha, HttpSession session) {
        Enter enter = new Enter() ;

        String captcha = (String) session.getAttribute(Constants.ADMIN_KAPTCHA_SESSION_KEY);
        // 添加一个万能验证码
        if( generalCaptcha.equals(inputCaptcha) ||  captcha.equalsIgnoreCase(inputCaptcha) ){
            enter.setCaptcha(true);
        }else {
            log.info("captcha验证码错误");
            enter.setBool(false);
            return enter ;
        }
        if(!StringUtils.hasLength(userInfo.getUserName()) || !StringUtils.hasLength(userInfo.getPassword())){
            enter.setBool(false);
            log.info("用户或密码为null");
            return enter ;
        }

        // 在数据库里查询用户密码是否正确 ;
         UserInfo check = userMapper.getUserByName(userInfo.getUserName()) ;
         if(check != null && check.getPassword().equals(userInfo.getPassword())) {
             enter.setBool(true);
             check.setPassword("");
             session.setAttribute(Constants.USERINFO_KEY , check);
         }
             return enter ;
    }

    public Object insertUserInfo(UserInfo userInfo, String inputCaptcha, HttpSession session) {
        Enter enter = new Enter() ;

        String captcha = (String) session.getAttribute(Constants.ADMIN_KAPTCHA_SESSION_KEY);
        if(captcha.equalsIgnoreCase(inputCaptcha) || generalCaptcha.equals(inputCaptcha)){
            enter.setCaptcha(true);
        }else {
            log.info("captcha验证码错误");
            enter.setBool(false);
            return enter ;
        }
         if(userMapper.getUserByName(userInfo.getUserName()) != null){
           enter.setBool(false);
           return enter ;
         }
         int check =  userMapper.insertUserInfo(userInfo);
         if(check > 0){
             enter.setBool(true);
         }
         return enter ;
    }

    public Object getUserInfo(HttpSession httpSession) {
         UserInfo userInfo = (UserInfo) httpSession.getAttribute(Constants.USERINFO_KEY);

         UserInfo result = new UserInfo() ;
        result.setUserId(userInfo.getUserId());
        result.setUserName(userInfo.getUserName());
        result.setHeadImage(userInfo.getHeadImage());
        return result ;
    }

    public Object refuse(Integer userId, Integer fromId) {
        return userMapper.refuse(userId , fromId);
    }

    public Object agree(Integer userId, Integer fromId) {
        userMapper.refuse(userId , fromId) ;
        friendMapper.addFriend(userId , fromId);
        return createSession(fromId , userId);
    }

   // 添加好友创建session
    public Object createSession(Integer friendId, Integer userId) {

        SessionUser sessionUser = new SessionUser() ;
        // session表里添加一个session, 然后获取自增主键
        Integer result =  sessionMapper.addSession(sessionUser) ;
        if(result < 1){
            log.error("添加会话错误");
            return Result.error("添加会话错误");
        }
        Integer result2 = sessionMapper.addSessionUser(
                sessionUser.getSessionId(), friendId,userId);
        if(result2 < 1){
            log.error("添加会话用户表错误");
            return Result.error("添加会话用户表错误");
        }
        return sessionUser.getSessionId();
    }
}
